<?php

namespace app\common\model;

use app\common\model\card\Dealer;
use think\Db;
use think\Model;


class Regionservice extends Model
{


    // 表名
    protected $name = 'regionservice';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];


    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    /**
     * 变更余额
     * @param int $money 余额
     * @param int $user_id 会员ID
     * @param string $memo 备注
     */
    public static function money($money, $account_id, $memo, $type_data = 1, $is_trila = 0)
    {
        if ($type_data == 4) {
            $account = Distributor::find($account_id);
        }
        if ($type_data == 1) {
            $account = Merchant::find($account_id);
            if ($is_trila == 1) {
                $account = Merchantuser::where(["is_trila" => 1])->find();
                $account_id = $account["id"];
            }
        }
        if ($type_data == 2) {
            $account = self::find($account_id);
        }
        if ($type_data == 3) {
            $account = Dealer::find($account_id);
        }
        if ($account && $money != 0) {
            $before = $account["money"];
            //$after = $user->money + $money;
            $after = function_exists('bcadd') ? bcadd($account->money, $money, 2) : $account->money + $money;
            //更新信息
            /*if ($type_data == 1) {
                $res = Merchant::where(["id"=>$account_id])->fetchSql()->update(['money' => $after]);
            }
            if ($type_data == 2) {
                $res = self::where(["id"=>$account_id])->update(['money' => $after]);
            }*/
            $res = $account->save(['money' => $after]);
            //写入日志
            $data = [
                'member_id' => $account_id,
                "type_data" => $type_data . '', 'money' => $money, 'before' => $before, 'after' => $after,
                'memo' => $memo,
                'createtime' => time(),
                'is_trila' => $is_trila,
            ];
            MemberMoneyLog::create($data);
        }
    }

}
